<?php
/**
 * Created by PhpStorm.
 * User: flutesing
 * Date: 15/8/13
 * Time: 下午5:02
 */

namespace Api\Controller;
use Think\Controller;

class ArticleController extends  Controller
{

    protected $newsModel;
    protected $newsDetailModel;
    protected $newsCategoryModel;

    function _initialize()
    {

        $this->newsModel = D('News/News');
        $this->newsDetailModel = D('News/NewsDetail');
        $this->newsCategoryModel = D('News/NewsCategory');

        $tree = $this->newsCategoryModel->getTree(0, true, array('status' => 1));
        $this->assign('tree', $tree);
        foreach ($tree as $category) {
            $menu = array('tab' => 'category_' . $category['id'], 'title' => $category['title'], 'href' => U('News/index/index', array('category' => $category['id'])));
            if ($category['_']) {
                $menu['children'][] = array('title' => '全部', 'href' => U('News/index/index', array('category' => $category['id'])));
                foreach ($category['_'] as $child)
                    $menu['children'][] = array('title' => $child['title'], 'href' => U('News/index/index', array('category' => $child['id'])));
            }
            $menu_list['left'][] = $menu;
        }
        $menu_list['right'] = array();
        if (is_login()) {
            $menu_list['right'][] = array('tab' => 'myNews', 'title' => '我的投稿', 'href' => U('News/index/my'));
        }

        $show_edit = S('SHOW_EDIT_BUTTON');
        if ($show_edit === false) {
            $map['can_post'] = 1;
            $map['status'] = 1;
            $show_edit = $this->newsCategoryModel->where($map)->count();
            S('SHOW_EDIT_BUTTON', $show_edit);
        }
        if ($show_edit) {
            $menu_list['right'][] = array('tab' => 'create', 'title' => '<i class="icon-edit"></i> 投稿', 'href' => is_login() ? U('News/index/edit') : "javascript:toast.error('登录后才能操作')");
        }

    }



    public function getIndex($page = 1, $r = 10)
    {
        /* 分类信息 */

        $category = 0 ;
        if ($category) {
            $this->_category($category);
            $cates = $this->newsCategoryModel->getCategoryList(array('pid' => $category, 'status' => array('gt', 0)));
            if (count($cates)) {
                $cates = array_column($cates, 'id');
                $cates = array_merge(array($category), $cates);
                $map['category'] = array('in', $cates);
            } else {
                $map['category'] = $category;
            }
        }
        $map['dead_line'] = array('gt', time());
        $map['status'] = 1;
        $map['position'] = array('gt', 0);

        /* 获取当前分类下资讯列表 */
        list($list, $totalCount) = $this->newsModel->getListByPage($map, $page, 'sort desc,update_time desc', '*', $r);
        foreach ($list as &$val) {
            $val['user'] = query_user(array('space_url', 'nickname'), $val['uid']);
        }
        unset($val);

        foreach ($list as $k => &$v) {
            if(check_document_position($v['position'], 1) === false) {
                unset($list[$k]);
                continue;
            }
            $v['cover'] = getThumbImageByCoverId($v['cover'],540,180);
        }




        if(IS_AJAX) {
            $json['data']['list'] = $list;
            $this->success('操作成功','',$json);
        }

        /* 模板赋值并渲染模板 */

        $this->assign('list', $list);
        $this->assign('category', $category);
        $this->assign('totalCount', $totalCount);

        $this->display();

    }


}